package cn.lyb.common;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.Data;

import javax.validation.constraints.Min;
import java.io.Serializable;

/**
 * 过滤参数
 *
 * @param <T>
 * @author lyb
 */
@Data
public class RequestFilterParam<T> implements Serializable {
    @Min(value = 1, message = "页码不能小于1")
    private int pageNum;
    @Min(value = 1, message = "每页的数据量不能小于1")
    private int pageSize;
    private T filter;
    private String orderType;
    private String orderColumn;
    private String keyWords;

    /**
     * 是否为ASC(正序)
     *
     * @return
     */
    public boolean isAsc() {
        return "asc".equals(orderType);
    }

    /**
     * 构建lambdaQueryWrapper
     *
     * @return
     */
    public LambdaQueryWrapper<T> buildLambdaQueryWrapper() {
        QueryWrapper<T> queryWrapper = new QueryWrapper<>();
        try {
            if (orderType != null && !orderType.equals("")) {
                queryWrapper.orderBy(true, isAsc(), getOrderColumn());
            }
        } catch (NullPointerException ignored) {

        }
        return queryWrapper.lambda();
    }
}